//
//  QSSignInRecordViewController.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/3/20.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSSignInRecordViewController.h"
#import "QSSignInRecordHeaderView.h"
#import "VTMagic.h"
#import "QSVTSignInRecordViewController.h"
#import "QSSingInInterface.h"
#import "VTMenuBar.h"
@interface QSSignInRecordViewController ()<VTMagicViewDataSource, VTMagicViewDelegate,LMJDropdownMenuDataSource,LMJDropdownMenuDelegate>
@property (nonatomic, strong)QSSignInRecordHeaderView *headerView;
@property (nonatomic, strong) VTMagicController *magicController;
@property (nonatomic, strong)  NSArray *menuList;
@property (nonatomic, strong) NSMutableArray *openList;
@property (nonatomic, strong) NSString *beforeMonthNums;
@property (nonatomic, strong) NSString *currentMonthNums;
@property (nonatomic, strong) NSString *todayNums;

@property (nonatomic, assign) NSInteger epId;
@property (nonatomic, assign) NSInteger egId;
//装有头部按钮的数组
@property (nonatomic, strong) NSMutableArray *menuItemArr;
@end


@implementation QSSignInRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F1F3F4"];
        
    
    [self configSubView];
    [self configVTItemArr];//配置item数组
    [self.view addSubview:self.magicController.view];
    [self.magicController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(kRealValue(10));
        make.top.equalTo(self.headerView.mas_top).offset(kRealValue(125 / 2 + 10));
        make.right.equalTo(self.view).offset(- kRealValue(10));
        make.bottom.equalTo(self.view);
    }];
    [self.view bringSubviewToFront:self.magicController.magicView];

    WeakSelf(weakSelf);
    
    self.headerView.recordHeaderViewDidFinishLoadOpenList = ^(NSArray * _Nonnull openList, NSString * _Nonnull egId, NSString * _Nonnull epId) {
        NSLog(@"ep=%@,eg=%@", epId, egId);
        if (kCurrentUser.enterpriseLevel.intValue == 2) {
            //组长把自己的组id取出  默认全组数据
            weakSelf.egId = egId.integerValue;
        }
        if (kCurrentUser.enterpriseLevel.intValue == 1) {
            //组员把自己的组id取出  默认自己的数据
            weakSelf.epId = epId.integerValue;
        }
        [weakSelf.openList addObjectsFromArray: openList];
        if (kCurrentUser.enterpriseLevel.intValue != 1) {
            //组员无头部切换
            [weakSelf configMenu];//创建切换视图
        }
//        [weakSelf.magicController.magicView reloadData];
        [weakSelf getAndUpdateRecordNumWithEpId:epId.integerValue EgId:egId.integerValue];
    };
}

- (void)configVTItemArr{
    for (int i = 0; i < 3; i++) {
        UIButton *menuItem;
        menuItem = [UIButton buttonWithType:UIButtonTypeCustom];
//        menuItem.tag = i;
        [menuItem setTitleColor:[UIColor colorWithHexString:@"666666"] forState:UIControlStateNormal];
        [menuItem setTitleColor:[UIColor colorWithHexString:@"#128BEC"] forState:UIControlStateSelected];
        menuItem.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0f];
        menuItem.titleLabel.numberOfLines = 0;
        menuItem.titleLabel.textAlignment = NSTextAlignmentCenter;
        NSLog(@"----%@", menuItem.currentTitle);
        [self.menuItemArr addObject:menuItem];
    }
}

- (void)configMenu{
    // ----------------------- menu1 ---------------------------
    [self.view addSubview:self.openMenu];
}

- (void)configSubView{
    [self.view addSubview:self.headerView];
    [_headerView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(kRealValue(10));
        make.top.equalTo(self.view).offset(kRealValue(10));
        make.right.equalTo(self.view).offset(- kRealValue(10));
        make.height.offset(kRealValue(125));
    }];
}

- (QSSignInRecordHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[QSSignInRecordHeaderView alloc]initWithFrame:CGRectZero];
    }
    return _headerView;
}

#pragma mark - 懒加载
- (VTMagicController *)magicController {
    if (!_magicController) {
        _magicController = [[VTMagicController alloc] init];
        _magicController.view.translatesAutoresizingMaskIntoConstraints = NO;
        _magicController.magicView.navigationColor = [UIColor QSColorWhiteFFFFFF];
        _magicController.magicView.sliderColor = [UIColor colorWithHexString:@"#28A6FF"];
        _magicController.magicView.switchStyle = VTSwitchStyleDefault;
        _magicController.magicView.layoutStyle = VTLayoutStyleDivide;
        _magicController.magicView.sliderWidth = kRealValue(28);
//        _magicController.magicView.separatorColor = [UIColor colorWithHexString:@"dddddd"];
        _magicController.magicView.navigationHeight = 74.f;
//        _magicController.magicView.againstStatusBar = NO;
        _magicController.magicView.sliderExtension = 10.0;
        _magicController.magicView.dataSource = self;
        _magicController.magicView.delegate = self;
        _magicController.magicView.layer.cornerRadius = 5;

        if (kCurrentUser.enterpriseLevel.integerValue == 1) {//服务员关闭预加载
            _magicController.magicView.needPreloading = NO;
        }
//        _magicController.magicView.frame = CGRectMake(10, kNavgationBarHeight, kScreenWidth - 20, kScreenHeight - kNavgationBarHeight - kTabBarHeight);

    }
    return _magicController;
}

- (NSMutableArray *)menuItemArr{
    if (!_menuItemArr) {
        _menuItemArr = [NSMutableArray array];
    }
    return _menuItemArr;
}

- (NSMutableArray *)openList{
    if (!_openList) {
//        {
//            egAdminId = 47;
//            egControllerId = 1;
//            egDrState = 0;
//            egId = 11;
//            egName = "天瑞组";
//            egRemark = "天瑞组";
//        }
        _openList = [NSMutableArray array];
        if (kCurrentUser.enterpriseLevel.integerValue == 2) {
            NSDictionary *dic = @{@"adminName" : @"全部",
                                             @"epId" : @""
                       };
            [_openList addObject:dic];
        }else{
//            return [self.openList[index] objectForKey:@"egName"];
            NSDictionary *dic = @{@"egName" : @"全部",
                                  @"egId" : @"",
            };
            [_openList addObject:dic];
        }
    }
    return _openList;
}
- (LMJDropdownMenu *)openMenu{
    if (!_openMenu) {
        _openMenu = [[LMJDropdownMenu alloc] init];
        [_openMenu setFrame:CGRectMake(kScreenWidth - kRealValue(150), 15, kRealValue(140), kRealValue(34))];
        _openMenu.dataSource = self;
        _openMenu.delegate   = self;
        
//        _openMenu.layer.borderColor  = [UIColor blackColor].CGColor;
//        _openMenu.layer.borderWidth  = 0.5;
//        _openMenu.layer.cornerRadius = 3;
        if (kCurrentUser.enterpriseLevel.integerValue == 2) {
            _openMenu.title = @"全部组员";
        }else{
            _openMenu.title = @"全部小组";
        }
        _openMenu.titleBgColor    = [UIColor QSColorWhiteFFFFFF];
        _openMenu.titleFont       = [UIFont QSFontOfSize14];
        _openMenu.titleColor      = [UIColor QSColorBlack434343];
        _openMenu.titleAlignment  = NSTextAlignmentRight;
        _openMenu.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 20);
        
        _openMenu.rotateIcon      = [UIImage imageNamed:@"ic_selectpeople"];
        _openMenu.rotateIconSize  = CGSizeMake(9, 6);
        
        _openMenu.optionBgColor       = [UIColor QSColorWhiteFFFFFF];
        _openMenu.optionFont          = [UIFont systemFontOfSize:14];
        _openMenu.optionTextColor     = [UIColor blackColor];
        _openMenu.optionTextAlignment = NSTextAlignmentCenter;
        _openMenu.optionNumberOfLines = 1;
        _openMenu.optionLineColor     = [UIColor blackColor];
        _openMenu.optionIconSize      = CGSizeMake(15, 15);
    }
    return _openMenu;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - VTMagicViewDataSource
- (NSArray<NSString *> *)menuTitlesForMagicView:(VTMagicView *)magicView {
    return _menuList;
}
    
- (UIButton *)magicView:(VTMagicView *)magicView menuItemAtIndex:(NSUInteger)itemIndex {
    static NSString *itemIdentifier = @"itemIdentifier";
    UIButton *menuItem = [magicView dequeueReusableItemWithIdentifier:itemIdentifier];
    if (!menuItem) {
//        menuItem = [UIButton buttonWithType:UIButtonTypeCustom];
//        [menuItem setTitleColor:[UIColor colorWithHexString:@"666666"] forState:UIControlStateNormal];
//        [menuItem setTitleColor:[UIColor colorWithHexString:@"#128BEC"] forState:UIControlStateSelected];
//        menuItem.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:16.0f];
//        menuItem.titleLabel.numberOfLines = 0;
//        menuItem.titleLabel.textAlignment = NSTextAlignmentCenter;
//        NSLog(@"----%@", menuItem.currentTitle);
//        NSMutableAttributedString *noteStr1 = [[NSMutableAttributedString alloc] initWithString:menuItem.currentTitle];
//        // 需要改变的区间(第一个参数，从第几位起，长度)
//        NSRange range = NSMakeRange(0, 3);
//        // 改变字体大小及类型
//        [noteStr1 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:range];
//        [menuItem setAttributedTitle:noteStr1 forState:UIControlStateNormal];
//        [menuItem.titleLabel setAttributedText:noteStr1];
//        NSLog(@"%@", self.menuItemArr);
        menuItem = [self.menuItemArr objectAtIndex:itemIndex];
    }
    return menuItem;
}
    
- (UIViewController *)magicView:(VTMagicView *)magicView viewControllerAtPage:(NSUInteger)pageIndex {
    
    QSVTSignInRecordViewController *kindFirstViewController = [[QSVTSignInRecordViewController alloc] init];
    kindFirstViewController.index = pageIndex;
    kindFirstViewController.companyModelArray = self.companyModelList;//赋值企业模型数组（服务员筛选企业）
    //切换之后传组员id/小组id
    NSLog(@"epid = %ld, egid = %ld", self.epId, self.egId);
    if (self.epId) {
        kindFirstViewController.epId = self.epId;
    }
    if (self.egId) {
        kindFirstViewController.egId = self.egId;
    }
    return kindFirstViewController;
}
#pragma mark - functional methods
- (void)generateTestData {
//    NSMutableAttributedString *todayNum = [[NSMutableAttributedString alloc]initWithString:[self.todayNums stringByAppendingString:@"\n\n今日签到"]];
//    NSRange range1 = [[todayNum string] rangeOfString:[self.todayNums substringFromIndex:self.todayNums.length]];
//    [todayNum addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range1];
//        [todayNum addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:range1];
    NSString *todayNum = [self.todayNums stringByAppendingString:@"\n\n今日签到"];
    NSString *currentMonthNums = [self.currentMonthNums stringByAppendingString:@"\n\n本月签到"];
    NSString *beforeMonthNums = [self.beforeMonthNums stringByAppendingString:@"\n\n上月签到"];
    
    
    _menuList = @[todayNum, currentMonthNums, beforeMonthNums];
}
//下拉控件item数量
#pragma mark - LMJDropdownMenu DataSource
- (NSUInteger)numberOfOptionsInDropdownMenu:(LMJDropdownMenu *)menu{
    return self.openList.count;
}
- (CGFloat)dropdownMenu:(LMJDropdownMenu *)menu heightForOptionAtIndex:(NSUInteger)index{
    return 34;
}
- (NSString *)dropdownMenu:(LMJDropdownMenu *)menu titleForOptionAtIndex:(NSUInteger)index{
    if (kCurrentUser.enterpriseLevel.integerValue == 2) {
        return [self.openList[index] objectForKey:@"adminName"];
    }else{
        return [self.openList[index] objectForKey:@"egName"];
    }
}

#pragma mark - LMJDropdownMenu Delegate
- (void)dropdownMenu:(LMJDropdownMenu *)menu didSelectOptionAtIndex:(NSUInteger)index optionTitle:(NSString *)title{
    NSInteger epId = 0 ;
    NSInteger egId = 0 ;
    NSString *companyKey;
    if (kCurrentUser.enterpriseLevel.integerValue == 2) {
        //组长取epid
        epId = [[self.openList[index] objectForKey:@"epId"] integerValue];
    }
    if(kCurrentUser.enterpriseLevel.integerValue == 3 || kCurrentUser.enterpriseLevel.integerValue == 4){
        //领导取egid
        egId = [[self.openList[index] objectForKey:@"egId"] integerValue];
    }
    NSLog(@"epid = %ld, egid = %ld", epId, egId);
    companyKey = @"";
    [self getAndUpdateRecordNumWithEpId:epId EgId:egId];
}

//请求数量
- (void)getAndUpdateRecordNumWithEpId:(NSInteger)epId
                                 EgId:(NSInteger)egId{
    [QSSingInInterface getApiSignNumsWithLevel:kCurrentUser.enterpriseLevel.intValue CompanyKey:@"" EpId:epId EgId:egId andBlock:^(ResponseMessage * _Nonnull rspStatusAndMessage, QSSignNumModel * _Nonnull result) {
        if (rspStatusAndMessage.code == kResponseSuccessCode) {
            NSLog(@"num:%@", result);
            self.epId = epId;
            self.egId = egId;
            self.beforeMonthNums = result.beforeMonthNums;
            self.currentMonthNums = result.currentMonthNums;
            self.todayNums = result.todayNums;

            [self generateTestData];//设置头部标题数组
            [self.magicController.magicView reloadData];
            
            [self changeItemTextFont];//改变
        }
    }];
}

- (void)changeItemTextFont{
    NSLog(@"~~~~%@", self.menuItemArr);
    for (UIButton *sender in self.menuItemArr) {
        NSLog(@"----ooo%@", sender.currentTitle);
         NSMutableAttributedString *noteStr1 = [[NSMutableAttributedString alloc] initWithString:sender.currentTitle];
         NSMutableAttributedString *noteStr2 = [[NSMutableAttributedString alloc] initWithString:sender.currentTitle];
        // 需要改变的区间(第一个参数，从第几位起，长度)
        NSRange range = NSMakeRange(0, 3);
        if (sender.tag == 1000) {
            range = NSMakeRange(0, self.todayNums.length);
        }else if (sender.tag == 1001){
            range = NSMakeRange(0, self.currentMonthNums.length);
        }else{
            range = NSMakeRange(0, self.beforeMonthNums.length);
        }
        sender.y = -10;
        // 改变字体大小及类型
        [noteStr1 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22 weight:0.1] range:range];
        [noteStr1 addAttribute:NSForegroundColorAttributeName value:[UIColor QSColorBlack434343] range:NSMakeRange(0, sender.currentTitle.length)];
        [noteStr2 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22 weight:0.1] range:range];
        [noteStr2 addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#128BEC"] range:NSMakeRange(0, sender.currentTitle.length)];
        [sender setAttributedTitle:noteStr1 forState:UIControlStateNormal];
        [sender setAttributedTitle:noteStr2 forState:UIControlStateSelected];
//       [sender.titleLabel setAttributedText:noteStr1];
        
        [sender setTitleColor:[UIColor colorWithHexString:@"666666"] forState:UIControlStateNormal];
        [sender setTitleColor:[UIColor colorWithHexString:@"#128BEC"] forState:UIControlStateSelected];
    }
}

@end
